package com.lagou.homework_1_3;

//模拟Arrays.toString(int[] arr);方法
public class ToString {

    public static void toString(int[] arr){

        //如果int类型数组arr为null，toString方法返回字符串"null"
        if (arr== null ){
            System.out.println("null");;
        }

        //如果int类型数组arr长度为0，toString方法返回字符串"[]"
        if (arr.length == 0){
            System.out.println("[]");
        }

        //如果int类型数组arr的内容为{1,2,3,4,5},toString方法返回字符串"[1, 2, 3, 4, 5]"
        if (arr.length > 0){
            StringBuilder str1 = new StringBuilder();
            str1.append("[");
            for (int i = 0; i <= arr.length-1; i++){
                Object obj = arr[i];
                if (arr.length-1 ==i){
                    str1.append(obj).append("]");
                }
                else{
                    str1.append(obj).append(",").append(" ");
                }
            }
            System.out.println(str1);
        }

    }

    //测试
    public static void main(String[] args) {

        int arr1[] = {1,2,3,4,5};

        int arr2[] = {};

        ToString.toString(arr2);
        ToString.toString(arr1);
    }
}
